﻿using Ec_System.Models.Web;
using Ec_System.Service.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ec_System.Bll.Web
{
   public class ProductClassManager
    {
        ProductClassService service = new ProductClassService();

        /// <summary>
        /// 查询所有的菜单信息
        /// </summary>
        /// <returns></returns>
        public  ProductClassData GetMenuData()
        {
            var menuData = service.GetMenuData();
            ProductClassData menuList = new ProductClassData();

            //获取一级菜单
            menuList.Class1 = menuData.Where(m => m.ParentID == 0).Distinct().ToList();
            //获取二级菜单
            menuList.Class2 = (from c1 in menuList.Class1
                               from c2 in menuData
                               where c2.ParentID == c1.ID
                               select c2).ToList();
            //获取三级菜单
            menuList.Class3 = (from c2 in menuList.Class2
                               from c3 in menuData
                               where c3.ParentID == c2.ID
                               select c3).ToList();
            return menuList;
        }

        /// <summary>
        /// 根据分类查找Product
        /// </summary>
        /// <param name="class1"></param>
        /// <param name="class2"></param>
        /// <param name="class3"></param>
        /// <returns></returns>
        public ProductClassData GetSearchCondition(int class1, int class2, int class3)
        {
            var menuData = service.GetMenuData();
            var menuList = new ProductClassData();

            menuList.Class1 = menuData.Where(m => m.ParentID == 0).Distinct().ToList();

            if(class1 == 0)
            {
                menuList.Class2 = (from c1 in menuList.Class1
                                   from c2 in menuData
                                   where c2.ParentID == c1.ID
                                   select c2).ToList();
            }
            else
            {
                menuList.Class2 = (from c2 in menuData
                                   where c2.ParentID == class1
                                   select c2).ToList();
            }

            if(class2 == 0)
            {
                menuList.Class3 = (from c2 in menuList.Class2
                                   from c3 in menuData
                                   where c3.ParentID == c2.ID
                                   select c3).ToList();
            }
            else
            {
                menuList.Class3 = (from c3 in menuData
                                   where c3.ParentID == class2
                                   select c3).ToList(); 
            }

            return menuList;
        }
    }
}
